﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for Usuario
/// </summary>
public partial class Usuario
{
    //public Usuario()
    //{
    //    //
    //    // TODO: Add constructor logic here
    //    //
    //}
    public static bool createUsuario(Usuario usuario)
    {
        DataClassesDataContext dc = new DataClassesDataContext();
        dc.Usuarios.InsertOnSubmit(usuario);
        dc.SubmitChanges();
        return true;
    }
    public static String createLogin(String nomePessoa)
    {
        DataClassesDataContext dc = new DataClassesDataContext();
        string[] splitNome = nomePessoa.Split(new char[] { ' ' }, 2);
        if (splitNome.Count() != 1)
        {
            string login = splitNome[0] + "." + splitNome[1];
            int cont = 1;
            while (existLogin(login))
            {
                login = login + cont;
                cont++;
            }
            return login;
        }
        else
        {
            string login = splitNome[0];
            int cont = 1;
            while (existLogin(login))
            {
                login = login + cont;
                cont++;
            }
            return login;
        }
    }
    public static bool acceptUsuario(Guid id)
    {
        try
        {
            DataClassesDataContext dc = new DataClassesDataContext();
            Usuario usuario = dc.Usuarios.Single(p => p.id.Equals(id));
            usuario.confirmado = true;
            dc.SubmitChanges();
            return true;
        }
        catch (Exception ex)
        {
            return false;
        }
    }
    public static bool existUsuario(Guid idUsuario)
    {
        DataClassesDataContext dc = new DataClassesDataContext();
        return dc.Usuarios.Any(p => p.id.Equals(idUsuario));
    }
    public static bool existLogin(String login)
    {
        DataClassesDataContext dc = new DataClassesDataContext();
        return dc.Usuarios.Any(p => p.login.Equals(login));
    }
    public static Usuario findUsuarioByIdPessoa(Guid idPessoa)
    {
        DataClassesDataContext dc = new DataClassesDataContext();
        return dc.Usuarios.Single(p => p.IdPessoa.Equals(idPessoa));
    }  

    public static Pessoa findPessoaById(Guid idUsuario)
    {
        DataClassesDataContext dc = new DataClassesDataContext();
        return dc.Usuarios.Single(p => p.id.Equals(idUsuario)).Pessoa;
    }
    /// <summary>
    /// kjhgfdsdvfbnijuhy
    /// </summary>
    /// <param name="Login">passar string com o ID do usuário</param>
    /// <param name="Senha"></param>
    /// <returns></returns>
    public static Usuario findByEmailSenha(String Login, String Senha)
    {
        DataClassesDataContext dc = new DataClassesDataContext();
        return dc.Usuarios.Single(p => p.login.Equals(Login) && p.senha.Equals(Senha));
    }
    public static Usuario findByEmail(String email)
    {
        DataClassesDataContext dc = new DataClassesDataContext();
        return dc.Usuarios.Single(p => p.email.Equals(email));
    }
    public static Usuario findById(Guid id)
    {
        DataClassesDataContext dc = new DataClassesDataContext();
        return dc.Usuarios.Single(p => p.id.Equals(id));
    }
    public static bool existLoginSenha(object Login, object Senha)
    {
        DataClassesDataContext dc = new DataClassesDataContext();
        return dc.Usuarios.Any(p => p.login.Equals(Login) && p.senha.Equals(Senha));
    }
    public static bool exist(String Login, String Email)
    {
        DataClassesDataContext dc = new DataClassesDataContext();
        return dc.Usuarios.Any(p => p.login.Equals(Login) || p.email.Equals(Email));
    }
    public static bool ativo(String Login)
    {
        DataClassesDataContext dc = new DataClassesDataContext();
        return dc.Usuarios.Any(p => p.login.Equals(Login) && p.confirmado.Equals(true));
    }
    public static bool exist(String Email)
    {
        DataClassesDataContext dc = new DataClassesDataContext();
        return dc.Usuarios.Any(p => p.email.Equals(Email));
    }
    public static void confirmarCadastro(Guid IdPessoa)
    {
        DataClassesDataContext dc = new DataClassesDataContext();
        Usuario usuario = dc.Usuarios.Single(p => p.IdPessoa.Equals(IdPessoa));
        usuario.confirmado = true;
        dc.SubmitChanges();
    }
    public static void confirmarSubmissaoArtigo(Guid IdPessoa, Guid IdArtigo)
    {
        DataClassesDataContext dc = new DataClassesDataContext();
        AutoresArtigo aa = dc.AutoresArtigos.Single(p => p.idPessoa.Equals(IdPessoa) && p.idArtigo.Equals(IdArtigo));
    }
}